/*
	YoghurtGum - 2D Game Engine

	Copyright 2009-2010 Quinten Lansu
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may not 
	use this file except in compliance with the License. You may obtain a copy 
	of the License at 
		
		http://www.apache.org/licenses/LICENSE-2.0 
		
	Unless required by applicable law or agreed to in writing, software 
	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
	License for the specific language governing permissions and limitations 
	under the License. 
*/

#ifndef _RENDERMETHODDIRECTDRAW_H_
#define _RENDERMETHODDIRECTDRAW_H_

#include "YoghurtGum.h"
#include "YGRenderMethod.h"
#include "YGRenderMethodManager.h"
#include "YGRoot.h"

#include "SourcesDirectDraw.h"
#include "ViewportDirectDraw.h"

#include "ErrorDirectDraw.h"

//#include "FreeImage.h"

namespace YoghurtGum
{

	struct ImageData
	{
		LPDIRECTDRAWSURFACE surf;
		DDSURFACEDESC desc;
		RECT dim;
		uint32 props;
		HDC handle;
		Color key;
	};

	struct ImageMap
	{
		char name[128];
		char file[128];
		ImageData* data;
		uint8 frames;
	};

	class RenderMethodDirectDraw : public RenderMethod
	{

	public:

		enum BufferMode
		{
			BUFFER_SINGLE,
			BUFFER_DOUBLE
		};

		~RenderMethodDirectDraw() { }

		static RenderMethodDirectDraw* GetSingleton();

		Surface* GetRenderTarget();

		unsigned long GetFillColor(Color a_Color);

		LPDIRECTDRAWSURFACE GetBackBuffer();
		uint32 GetBackbufferWidth();
		uint32 GetBackbufferHeight();
		LPDIRECTDRAW GetObject()  { return m_DDObject; }
		int GetPixelBytes()       { return m_DDPixelBytes; }
		int* GetDepthBuffer()     { return m_DDDepthBuffer; }
		HWND GetWindowHandle()    { return m_WindowHandle; }

		bool Init();
		void CleanUp();

		void SetClearColor(Color a_Color);
		void SetFullScreen();

		char* GetName();

		bool Clear();
		bool PreRender();
		bool PostRender();

		bool LoadImage(const char* a_File, const char* a_Name, Color a_AlphaKey = Color::CNONE);

		Sprite* CreateSprite(const char* a_Name);
		SpriteEx* CreateSpriteEx(const char* a_Name, uint32 a_Flags = YG_SPRITEEX_DEFAULT);
		Background* CreateBackground(const char* a_Name, uint32 a_Flags = YG_BACKGROUND_DEFAULT);
		Surface* CreateSurface(int a_Width, int a_Height, uint32 a_Flags = YG_SURFACE_DEFAULT);

		uint32 CreateViewport(uint32 a_Width, uint32 a_Height, uint32 a_Type = YG_VIEWPORT_DEFAULT);
		Viewport* GetViewport(uint32 a_ID);
		bool SetViewport(uint32 a_ID);

	private:

		RenderMethodDirectDraw();
		
		ImageMap              m_ImageData[128];
		uint32                m_ImageDataFilled;

		BufferMode            m_BufferMode;

		LPDIRECTDRAW          m_DDObject;
		LPDIRECTDRAWSURFACE   m_DDFront;
		LPDIRECTDRAWSURFACE   m_DDBack;

		int*                  m_DDDepthBuffer;
		Surface*              m_DDFrontBuffer;
		Surface*              m_DDBackgroundBuffer;
		Surface*              m_DDBackBuffer;
		DDPIXELFORMAT         m_DDPixelFormat;
		int                   m_DDPixelBytes;
		DDBLTFX               m_DDClearSettings;
		LPDIRECTDRAWCLIPPER   m_DDClipper;
		DDSURFACEDESC         m_DDSurfaceDesc;
		int                   m_DDBackPitch;
		int                   m_DDFrontPitch;
		DDCAPS                m_DDCaps;
		bool                  m_Mapped;
		RECT                  m_Dimensions;

		static RenderMethodDirectDraw* s_Instance;

	}; // class RenderMethodDirectDraw

}; // namespace YoghurtGum

#endif